<template>
  <div class="questionInto" v-if="bol">
    <hmNavBar
      title="刷题"
      path="/home/question"
      right="答题卡"
      @rightClick="rightClick"
    ></hmNavBar>
    <div class="content" v-if="list.length && list[currIndex].detail">
      <div class="title">
        <span>[{{ typeobj[list[currIndex].detail.type] }}]</span>
        {{ list[currIndex].detail.title }}
      </div>
      <van-tag color="#f7f4f5" text-color="#b7b7c0" class="tag"
        >教育培训</van-tag
      >
      <van-tag color="#f7f4f5" text-color="#b7b7c0" class="tag">人文</van-tag>
      <!-- 单选 -->
      <ul class="select" v-if="typeobj[list[currIndex].detail.type] === '单选'">
        <!-- 点击事件选中高亮
        正确答案,满足有答案属性,选中的答案等于str对应的index
        错误答案,满足有答案属性,选中的答案不等于答案,选中的答案等于str对应的Index -->
        <li
          v-for="(item, index) in list[currIndex].detail.option"
          :key="index"
          class="selectItem iconfont"
          @click="activeEvent(index)"
          :class="{
            active: ans1 === str[index],
            right:
              list[currIndex].answer &&
              list[currIndex].answer.singleAnswer === str[index],
            error:
              list[currIndex].answer &&
              list[currIndex].answer.singleAnswer !== ans1 &&
              ans1 === str[index]
          }"
        >
          {{ str[index] }}. {{ item }}
        </li>
      </ul>
      <!-- 多选 -->
      <ul class="select" v-else-if="typeobj[list[currIndex].detail.type] === '多选'">
        <!-- 点击事件选中高亮
        正确答案,满足有答案属性,选中的答案包含当前的选取str对应的index -->
        <li
          v-for="(item, index) in list[currIndex].detail.option"
          :key="index"
          class="selectItem iconfont"
          @click="activeEvent2(index)"
          :class="{
            active: ans2.includes(str[index]),
            right:
              list[currIndex].answer &&
              list[currIndex].answer.multipleAnswer.includes(str[index])
          }"
        >
          {{ str[index] }}. {{ item }}
        </li>
      </ul>
      <!-- 简答 -->
      <van-field
        v-else
        v-model="ans3"
        rows="5"
        type="textarea"
        @input="inputEvent"
        :disabled="step > 2"
        class="field"
      >
      </van-field>
    </div>
    <!-- 步骤大于二才渲染 -->
    <div class="answer" v-if="step > 2 && list[currIndex].answer">
      <h3 class="title">答案解析</h3>
      <div class="selectAnswer">
        正确答案:
        <span v-if="typeobj[list[currIndex].detail.type] === '单选'">{{
          list[currIndex].answer.singleAnswer
        }}</span>
        <span v-if="typeobj[list[currIndex].detail.type] === '多选'">{{
          list[currIndex].answer.multipleAnswer.join(',')
        }}</span>
      </div>
      <div class="middle">
        <div class="m1">
          难度: {{ difficultyObj[list[currIndex].answer.difficulty] }}
        </div>
        <div class="m2">提交次数: {{ list[currIndex].answer.submitNum }}</div>
        <div class="m3">正确次数: {{ list[currIndex].answer.correctNum }}</div>
      </div>
      <div class="answertxt">
        {{ list[currIndex].answer.answerAnalysis }}
      </div>
    </div>
    <div class="bottom">
      <div class="b1">
        <i class="iconfont">&#xe649;</i>
        <div class="txt">收藏</div>
      </div>
      <div class="b1">
        <i class="iconfont">&#xe606;</i>
        <div class="txt">反馈</div>
      </div>
      <div class="b2">
        <span class="num1">{{ currIndex + 1 }}</span
        ><span class="num2">/{{ list.length }}</span>
      </div>
      <van-button
        @click="submit"
        class="b3"
        type="danger"
        v-if="step < 3"
        :disabled="step === 1"
        >提交</van-button
      >
      <van-button
        @click="nextEvent"
        class="b3"
        type="danger"
        v-else-if="step === 3"
        >下一题</van-button
      >
      <van-button class="b3" type="danger" v-else>结束</van-button>
    </div>
    <!-- 答题卡 -->
    <van-popup v-model="showCard" position="bottom">
      <ul class="card">
        <li
          @click="liClick(index)"
          class="item-card"
          v-for="(item, index) in list"
          :key="index"
          :class="{
            right: item.answer && item.answer.isRight,
            error: item.answer && !item.answer.isRight
          }"
        >
          {{ index + 1 }}
        </li>
        <li class="nouse"></li>
        <li class="nouse"></li>
        <li class="nouse"></li>
        <li class="nouse"></li>
        <li class="nouse"></li>
        <li class="nouse"></li>
      </ul>
    </van-popup>
  </div>
</template>
<script>
import {
  interviewQuestions,
  questionsSubmit,
  questionsId
} from '@/api/question'

export default {
  data () {
    return {
      // 步骤为1不可提交,可以选择答案
      // 选择答案后步骤到2可以提交
      // 提交后步骤到3不可在更改答案,下一题
      // 最后一题步骤4,结束按钮

      step: 1,
      type: this.$route.query.type,
      city: this.$route.query.city,
      list: [],
      //   题目对应索引默认0
      currIndex: 0,
      //   答题类型
      typeobj: {
        1: '单选',
        2: '多选',
        3: '简答'
      },
      //   选项
      str: 'ABCDEFG',
      //   单选答案
      ans1: '',
      ans2: [],
      ans3: '',
      // 静默刷新
      bol: true,
      // 难度
      difficultyObj: {
        1: '简单',
        2: '中等',
        3: '困难'
      },
      // 弹出层
      showCard: false
    }
  },
  async created () {
    const res = await interviewQuestions({ type: this.type, city: this.city })
    console.log(res)
    this.list = res.data.data
  },
  methods: {
    activeEvent (index) {
      // 只有步骤小于3才可以选中
      // 选中高亮并且步骤到二,可以提交
      //   存储选中的答案,后台需要的是AB的格式
      if (this.step < 3) {
        this.ans1 = this.str[index]
        this.step = 2
        console.log(this.ans1)
      }
    },
    // 多选
    activeEvent2 (index) {
      // 遍历数组,如果等于-1添加进ans2,不等于-1删除
      if (this.step < 3) {
        const _index = this.ans2.indexOf(this.str[index])
        if (_index === -1) {
          this.ans2.push(this.str[index])
        } else {
          this.ans2.splice(_index, 1)
        }
      }
      // 如果多选数组有内容到阶段2,没有内容回到阶段1
      if (this.ans2.length > 0) {
        this.step = 2
      } else {
        this.step = 1
      }
    },
    // 简答
    inputEvent () {
      if (this.ans3.length > 0) {
        this.step = 2
      } else {
        this.step = 1
      }
    },
    // 提交
    async submit () {
      this.$toast.loading({
        duration: 0
      })
      const res = await questionsSubmit({
        id: this.list[this.currIndex].id,
        singleAnswer: this.ans1,
        multipleAnswer: this.ans2
      })
      this.$toast.clear()
      console.log(res)
      //   把放回来的答案数据添加到list里
      this.list[this.currIndex].answer = res.data.data
      this.list[this.currIndex].ans1 = this.ans1
      this.list[this.currIndex].ans2 = this.ans2
      this.list[this.currIndex].ans3 = this.ans3
      // 提交时判断是否是最后一题最后一题到步骤四否则3
      if (this.currIndex === this.list.length - 1) {
        this.step = 4
      } else {
        this.step = 3
      }
      //   渲染数据,静默刷新,步骤到3
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    },
    // 下一题
    async nextEvent () {
      //  索引++
      this.currIndex++
      // this.$toast.loading({
      //   duration: 0
      // })

      // const res = await questionsId(this.list[this.currIndex].id)
      // this.$toast.clear()

      // console.log(res)
      // this.list[this.currIndex].detail = res.data.data
      // // 清空所有的答案
      // this.ans1 = ''
      // this.ans2 = []
      // this.ans3 = ''
      // this.step = 1
      this.liClick(this.currIndex)
    },
    // 答题卡
    rightClick () {
      this.showCard = true
    },
    // 点击答题卡
    async liClick (index) {
      // 当前索引等于index
      this.currIndex = index
      // 点击关掉弹出层
      this.showCard = false

      //    如果没有题目
      //    获取题目 step=1
      // 如果有题目
      //     有答案
      //          状态处理  step=3/4
      //          有答案
      //          显示渲染
      //     没有答案
      //           step=1
      //           状态处理

      // 清空答案
      this.ans1 = ''
      this.ans2 = []
      this.ans3 = ''
      if (!this.list[this.currIndex].detail) {
        // 发送请求
        const res = await questionsId(this.list[this.currIndex].id)

        this.list[this.currIndex].detail = res.data.data
        this.step = 1
      } else {
        if (this.list[this.currIndex].answer) {
          if (this.currIndex === this.list.length - 1) {
            this.step = 4
          } else {
            this.step = 3
          }
          // 恢复答案
          this.ans1 = this.list[this.currIndex].ans1
          this.ans2 = this.list[this.currIndex].ans2
          this.ans3 = this.list[this.currIndex].ans3
        } else {
          this.step = 1
        }
      }
      // 静默刷新
      this.bol = false
      this.$nextTick(() => {
        this.bol = true
      })
    }
  }
}
</script>
<style lang="less" scoped>
.questionInto {
  padding: 0 @p15;
  .content {
    .title {
      padding-top: 25px;
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: justify;
      color: #181a39;
      line-height: 22px;
    }
    .tag {
      margin-top: 10px;
      margin-left: 13px;
      padding: 5px;
      margin-bottom: 28px;
    }
    .select {
      padding: 15px;
      .selectItem {
        font-size: 16px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        text-align: justify;
        color: #181a39;
        line-height: 22px;
        padding: 10px 15px;
        margin-bottom: 15px;
      }
    }
    .field {
      background-color: #f7f4f5;
    }
    .active {
      border: 1px solid #ccc;
    }
    .right {
      background-color: #ddfad9;
      color: green;
      position: relative;
      &::after {
        content: '\e604';
        color: green;
        font-size: 24px;
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
      }
    }
    .error {
      background-color: #ffefea;
      color: #181a39;
      position: relative;
      &::after {
        content: '\e605';
        color: #ff4949;
        font-size: 24px;
        position: absolute;
        right: 10px;
        top: 50%;
        transform: translateY(-50%);
      }
    }
  }
  .answer {
    .title {
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Medium;
      font-weight: 500;
      text-align: left;
      color: #222222;
      line-height: 25px;
      letter-spacing: 0px;
      margin-bottom: 18px;
    }
    .selectAnswer {
      font-size: 18px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: justify;
      color: #1dc779;
      line-height: 22px;
      margin-bottom: 19px;
    }
    .middle {
      width: 100%;
      background-color: #f7f4f5;
      display: flex;
      justify-content: space-between;
      font-size: 12px;
      padding: 8px 10px;
      margin-bottom: 20px;
    }
    .answertxt {
      font-size: 16px;
      font-family: PingFangSC, PingFangSC-Regular;
      font-weight: 400;
      text-align: justify;
      color: #181a39;
      line-height: 22px;
      padding-bottom: 80px;
    }
  }
  .bottom {
    padding: 5px @p15;
    position: fixed;
    bottom: 0;
    left: 0;
    display: flex;
    align-items: center;
    width: 100%;
    height: 60px;
    text-align: center;
    background-color: #fff;
    .b1 {
      flex: 1;
      .iconfont {
        font-size: 23px;
      }
      .txt {
        font-size: 12px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        color: #b4b4bd;
        line-height: 16px;
        letter-spacing: 0px;
      }
    }
    .b2 {
      flex: 2;
      .num1 {
        font-size: 22px;
        font-family: PingFangSC, PingFangSC-Regular;
        font-weight: 400;
        color: #e40137;
        line-height: 31px;
        letter-spacing: 1px;
      }
      .num2 {
        font-size: 16px;
        color: #5a5c76;
      }
    }
    .b3 {
      flex: 2;
    }
  }
  .card {
    padding: @p15;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    height: 300px;

    text-align: center;
    width: 100%;
    .item-card {
      width: 33px;
      height: 33px;
      line-height: 33px;
      opacity: 1;
      background: #ddfad9;
      border-radius: 50%;
      margin: 10px;
    }
    .nouse {
      width: 33px;
      height: 0px;
      line-height: 33px;
      opacity: 1;
      background: #ddfad9;
      border-radius: 50%;
      margin: 10px;
    }
    .right {
      background-color: #ddfad9;
      color: #66da9d;
    }
    .error {
      background-color: #ffefea;
      color: #ff4949;
    }
  }
}
</style>
